<?php
/*
 * ==BeLean==
 * This file is part of the BeLean project!
 * 
 * BBS Brinkstrasse Osnabrück
 * @author	Marco Kreienbrink <marco.kreienbrink@googlemail.com>
 *  
 */
 
 include_once "basic.class.php";
 
 class user extends basic{
	private $teacherTable = "teacher";
	private $tokenTable = "token";
	private $oDB;
	
	/**
	 * Default constructor, instatiates the parent object
	 */
 	function __construct(&$config){
		parent::__construct($config);
 	}
	
	/**
	 * Sets the database Object for further use
	 * 
	 * @param object $oDB DatabaseObject
	 */
	function setDB(&$oDB){
		if(is_object($oDB)){
			$this->oDB = $oDB;
		}
	}
	
	/**
	 * Adds a new Teacher with name $sName and Token $sToken.
	 * If no Token is passed it will be created on the fly.
	 * 
	 * @param string $sName
	 * @param string $sToken 
	 * @return bool int $iTeacherId|false
	 */
	function addNewTeacher($sName, $sToken=""){	
		if(!$this->nameAvailable($sName)){
			return -1;
		}	
		$sSQL = "INSERT into ".$this->teacherTable." (name) value('".$this->oDB->dbEscapeString($sName)."')";
		$this->oDB->executeUpdate($sSQL);
		if($this->oDB->getNumRow()>0){
			if(trim($sToken) == ""){
				$sToken = substr(md5($sName. date("H:i:s")),0,$this->oConfig->getValue('tokenLength'));
			}
			$sSQL = "INSERT into ".$this->tokenTable." (token, teacher_id) values(UPPER('$sToken'),'".$this->oDB->getLastInsertId()."')";
			$this->oDB->executeUpdate($sSQL);
			return true;
		}else{
			return false;
		}
	}
	
	function nameAvailable($sName){
		$sSQL ="SELECT id from ".$this->teacherTable." where name ='$sName'";
		$this->oDB->executeQuery($sSQL);
		if($this->oDB->getNumRow()>0){
			return false;
		}else{
			return true;
		}
	}
	
	/**
	 * Deletes a given Teacher
	 * 
	 * @param int $iTeacherId
	 */
	function deleteTeacher($iTeacherId){
		$sSQL = "DELETE from ".$this->teacherTable." where id = $iTeacherId";
		$this->oDB->executeUpdate($sSQL);
		$sSQL = "DELETE from ".$this->tokenTable." where teacher_id = $iTeacherId";
		$this->oDB->executeUpdate($sSQL);
	}

	/**
	 * Changes the Token for a teacher
	 * 
	 * @param int iTeacherId
	 * @param string $sNewToken
	 * @return bool true|false
	 */
	function changeTeachersToken($iTeacherId, $sNewToken){
		$sSQL = "UPDATE ".$this->tokenTable." set token = '$sNewToken' where teacher_id  = $iTeacherId";		
		$this->oDB->executeUpdate($sSQL);
		if($this->oDB->getNumRow()>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * Blocks / unblocks a teachers access
	 * 
	 * @param int $iTeacherId
	 * @return bool true|false
	 */
	function blockTeacher($iTeacherId, $iBlock=1){
		$sSQL = "UPDATE".$this->teacherTable." set blocked = $iBlocked where id = $iTeacherId";
		$this->oDB->executeUPdate($sSQL);
		if($this->oDB->getNumRow()>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * Grants acces to a previously blocked teacher
	 * 
	 * @param int $iTeacherId
	 */
	function unBlockTeacher($iTeacherId){
		return $this->blockTeacher($iTeacherId, 0);
	}
	
	/**
	 * Returns the details about a given Teacher
	 * 
	 * @param int $iTeacherId
	 * @return array mixed
	 */
	function getTeacherDetail($iTeacherId){
		$sSQL = "SELECT name from ".$this->teacherTable." where id = $iTeacherId";
		$this->oDB->executeQuery($sSQL);
		$res = $this->oDB->getRow();
		return $res['name'];
	}
	
	/**
	 * returns an array with all the teachers
	 * @param void
	 * @return mixed array
	 */
	function getTeacherList(){
		$sSQL = "SELECT id,name from ".$this->teacherTable." where blocked = 0";
		$this->oDB->executeQuery($sSQL);
		$ret = $this->oDB->getRows();
		return $ret;
	}
	
	function isAdminPass($sPass){
		$sPass = htmlentities(strip_tags(($sPass)));
		if($this->oConfig->getValue('adminPass')==$sPass){
			$_SESSION['is_admin'] = true;
			return true;
		}else{
			return false;
		}
	}
	
	function getTeacherPass($sTeacher){
		$sSQL = 'SELECT token FROM '.$this->tokenTable.' where token.teacher_id = (select id from '.$this->teacherTable.' where name = "'.$sTeacher.'")';
		$this->oDB->executeQuery($sSQL);
		return $this->oDB->getRow();
	}
	/**
	 * Returns an array with all the teachers associated with a given form
	 * @param int $iFormId
	 * @return mixed array
	 */
	function getTeacherByForm($iFormId){
		
	}
 }
?>